package com.gwl.concurrent.atomic;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerTest implements Runnable {

	private AtomicInteger value = new AtomicInteger(0); 
	@Override
	public void run() {
		while(true) 
			value.addAndGet(2); 
	} 
	
	public static void main(String[] args) {
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				System.out.println("Aborting"); 
				System.exit(0);  
			} 
		}, 5000);  // Terminate after 5 seconds
		
		ExecutorService exec = Executors.newCachedThreadPool(); 
		AtomicIntegerTest task = new AtomicIntegerTest(); 
		exec.execute(task); 
		
		while(true)  
			if(task.value.get() % 2 != 0) { 
				System.out.println("value not even!"); 
				System.exit(-1); 
			}
	}

}
